package org.computate.model.item.field;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.util.DateUtils;
import org.computate.form.ComputateForm;



public class ItemField {
	
	private Class<?> c;
	private String name;
	private String label;
	private String description;
	private boolean textarea = false;
	private boolean wysiwyg = false;
	private Integer header = null;
	private String itemName;
	private String itemDisplayName;
	private String itemPluralName;
	private String parentField = null;
	private boolean inIndex = false;
	private boolean link = false;
	private Integer stars = null;
	private boolean approved = true;
	
	@Override
	public boolean equals(Object obj) {
		if(obj != null && obj instanceof ItemField) {
			ItemField i = (ItemField)obj;
			
			if(StringUtils.equals(i.getName(), getName()))
				return true;
		}
		
		return false;
	}
	
	public Class<?> getC() {
		return c;
	}

	
	public void setC(Class<?> c) {
		this.c = c;
	}
	
	public String getName() {
		return name;
	}
	
	public ItemField setName(String name) {
		this.name = name;
		
		return this;
	}
	
	public String getLabel() {
		return label;
	}
	
	public ItemField setLabel(String label) {
		this.label = label;
		
		return this;
	}
	
	public String getDescription() {
		return description;
	}
	
	public ItemField setDescription(String description) {
		this.description = description;
		
		return this;
	}

	public Object getValue(ComputateForm computateForm) {
		Object result = null;
		
		if(computateForm != null) {
			try {
				Method getter = new PropertyDescriptor(name, computateForm.getClass()).getReadMethod();
				
				result = getter.invoke(computateForm);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IntrospectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	
	public String getValueString(ComputateForm computateForm) {
		String resultString = null;
		Object resultObject = getValue(computateForm);
		
		if(resultObject != null) {
			if(resultObject instanceof String) {
				resultString = (String)resultObject;
			}
			else if(resultObject instanceof Date) {
				Date result = (Date)resultObject;
				resultString = DateUtils.format(result, "EEE MMM d, yyyy");
			}
			else if(resultObject instanceof Boolean) {
				Boolean result = (Boolean)resultObject;
				resultString = result.toString();
			}
		}
		
		return resultString;
	}

	public boolean isTextarea() {
		return textarea;
	}

	public ItemField setTextarea(boolean textarea) {
		this.textarea = textarea;
		
		return this;
	}


	public boolean isWysiwyg() {
		return wysiwyg;
	}


	public void setWysiwyg(boolean wysiwyg) {
		this.wysiwyg = wysiwyg;
	}


	public Integer getHeader() {
		return header;
	}


	public ItemField setHeader(Integer header) {
		this.header = header;
		
		return this;
	}


	public String getItemName() {
		return itemName;
	}


	public ItemField setItemName(String itemName) {
		this.itemName = itemName;
		
		return this;
	}


	public String getItemDisplayName() {
		return itemDisplayName;
	}


	public ItemField setItemDisplayName(String itemDisplayName) {
		this.itemDisplayName = itemDisplayName;
		
		return this;
	}


	public String getItemPluralName() {
		return itemPluralName;
	}


	public ItemField setItemPluralName(String itemPluralName) {
		this.itemPluralName = itemPluralName;
		
		return this;
	}


	public String getParentField() {
		return parentField;
	}


	public ItemField setParentField(String parentField) {
		this.parentField = parentField;
		
		return this;
	}


	public boolean isInIndex() {
		return inIndex;
	}


	public ItemField setInIndex(boolean inIndex) {
		this.inIndex = inIndex;
		
		return this;
	}


	public boolean isLink() {
		return link;
	}


	public ItemField setLink(boolean link) {
		this.link = link;
		
		return this;
	}

	public Integer getStars() {
		return stars;
	}

	public ItemField setStars(Integer stars) {
		this.stars = stars;
		
		return this;
	}

	public boolean isApproved() {
		return approved;
	}

	public ItemField setApproved(boolean approved) {
		this.approved = approved;
		
		return this;
	}
}
